﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IISLogAnalyzer
{
    public partial class Form1 : Form
    {
        private FolderFileInfo FFI = new FolderFileInfo();
        private List<IISInfo> LstIISInfo = new List<IISInfo>();
        private List<string> LstServiceInfo = new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_FolderRegist_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            if (!string.IsNullOrEmpty(folderBrowserDialog1.SelectedPath))
            {
                FolderPathBox.Text = folderBrowserDialog1.SelectedPath;
            }
            else
            {
                return;
            }
            refreshComboBox();
        }

        private void btnFolderRefresh_Click(object sender, EventArgs e)
        {
            refreshComboBox();
        }
        private void refreshComboBox()
        {
            FFI = new FolderFileInfo();
            FFI.DirecctoryPath = FolderPathBox.Text;
            FileListBox.DataSource = FFI.Listfilename;
            //FileListBox.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Analyze();
        }
        private void Analyze()
        {
            LstIISInfo = GetIISInfo.GetInfo(FFI.DirecctoryPath + "\\" + FileListBox.SelectedItem.ToString());
            LstServiceInfo = GetServiceInfo.GetServiceInfoLst();
            textBox1.Text = "";
            foreach (string strService in LstServiceInfo)
            {
                textBox1.Text += "サービス名" + " \t" + strService + "\r\n";
                textBox1.Text += "時刻" + " \t" + "件数" + "\r\n";
                for (int i = 0; i < 24; i++)
                {
                    var ret =
                        (from p in LstIISInfo
                         where 
                         p.time >= new DateTime(p.time.Year, p.time.Month, p.time.Day, i, 0, 0) &&
                         p.time < new DateTime(p.time.Year, p.time.Month, p.time.Day, i + 1, 0, 0) &&
                         p.detail.Contains(strService.ToLower())
                         select p).Count();
                    textBox1.Text += i.ToString() + " \t" + ret.ToString() + "\r\n";
                }
                
            }
        }
    }
}
